import { createApp } from 'vue'
import './style.css'
import App from './App.vue'

//引入路由的封装
import router from './routers/index'

//引入VantUI框架
import Vant from 'vant'
import 'vant/lib/index.css'

// 引入自己封装的服务插件
import tools from '@/services/tools.js'

//引入封装的请求插件
import request from '@/services/request.js'

// 引入cookies
import VueCookies from 'vue3-cookies'

//pinia 状态管理进行挂载
import { createPinia } from 'pinia'//引入pinia
import piniaPluginPersist from 'pinia-plugin-persist'//引入pinia数据持久化插件

// 引入pinia数据
import ShopStore from '@/store/shop.js'

const app = createApp(App)
    .use(router)
    .use(Vant)
    .use(request)
    .use(createPinia().use(piniaPluginPersist))
    .use(ShopStore)
    .use(VueCookies)
    .use(tools)
    .mount('#app')


//定义路由前置钩子
// to 去哪里
// from 从哪里来
router.beforeEach(async (to, from) => {
    //获取是否需要登录的属性
    var IsLogin = to.meta.IsLogin ? to.meta.IsLogin : false;

    if(IsLogin === true)
    {
        //需要验证登录
        let business = app.$business.value ? app.$business.value : {}

        //判断是否有登录信息 提取对象中属性 如果属性为空 说明对象是空的
        if(!business || Object.getOwnPropertyNames(business).length <= 0)
        {
            app.$fail('请先登录')
            return {path: '/business/login'}
        }

        //验证登录信息
        var result = await app.$check();

        //没有登录成功
        if(result.code == 0)
        {
            app.$fail(result.msg)
            return {path: '/business/login'}
        }
    }
})